package com.mingle.iwalk;

import java.util.Date;
import java.util.List;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.Overlay;
import com.baidu.mapapi.OverlayItem;

/*import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;*/
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MapInfoActivity extends MapActivity {

	private POIInfo mInfo;
	private MapView mMapView;
	private List<Overlay> mMapOverlays; 
	private MapController  mMapController;
	
	private BMapManager mBmapMan;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.map_info_layout);
        
        Button exitBtn = (Button) findViewById(R.id.exit_btn);
        exitBtn.setOnClickListener(mExitListener);
        TextView title = (TextView) findViewById(R.id.theme);
        TextView time = (TextView) findViewById(R.id.time);

        TravelerDB travelerDB = TravelerDB.getInstance(getContext());
        
        int id = getIntent().getIntExtra("INFO_ID", TravelerInfo.INFO_INVALID_ID);
        
        if(id != TravelerInfo.INFO_INVALID_ID)
        {
        	mInfo = (POIInfo) travelerDB.getTravelerInfo(id, TravelerInfo.INFO_TYPE_POI);
		}
        else
        {
        	Intent i = getIntent();
        	double lat = i.getDoubleExtra("LAT", 0);
        	double lon = i.getDoubleExtra("LON", 0);
        	double alt = i.getDoubleExtra("ALT", 0);
        	long createTime  = i.getLongExtra("TIME", 0);
        	
        	if(lat == 0 || lon == 0 )
        	{
        		finish();
        		return;
        	}
        	
        	mInfo = new POIInfo();
        	mInfo.createTime = createTime;
        	mInfo.location = new Location(LocationManager.GPS_PROVIDER);
        	mInfo.location.setLatitude(lat);
        	mInfo.location.setLongitude(lon);
        	mInfo.location.setAltitude(alt);
        }
        
        
			if(mInfo == null)
			{
				finish();
				return;
			}
			
			title.setText(R.string.view_loc);
			String strTime = new Date(mInfo.createTime).toLocaleString();
			time.setText(strTime);
			
			mBmapMan = new BMapManager(getApplication());
			mBmapMan.init("15CEF55B47A59959AB2325755DD60E1F17A2ACBE", null);
			super.initMapActivity(mBmapMan);
			
			mMapView = (MapView) findViewById(R.id.map_view);
			
			mMapView.setBuiltInZoomControls(true);
			mMapView.setClickable(true);
		      
	        mMapOverlays = mMapView.getOverlays(); 
	        mMapController = mMapView.getController();
	        
	        if(mInfo.type == TravelerInfo.INFO_TYPE_POI)
	        {
	        	prepareMap(mInfo.location.getLatitude(), mInfo.location.getLongitude(), mInfo.location.getAccuracy());
	        }
		
	}
	
	private Context getContext()
	{
		return MapInfoActivity.this;
	}
	
	private void prepareMap(double lat, double lon, double accu)
	{
		GeoPoint orig_point=new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6));
		Resources res  = getContext().getResources();
		String snip = res.getString(R.string.lon)+": "+mInfo.location.getLongitude() + res.getString(R.string.degree_unit) + "\n"
		+ res.getString(R.string.lat)+": "+mInfo.location.getLatitude() + res.getString(R.string.degree_unit) + "\n"
		+ res.getString(R.string.alt)+": "+mInfo.location.getAltitude() + res.getString(R.string.distance_unit) + "\n";
		
		OverlayItem overlayitem = new OverlayItem(orig_point, res.getString(R.string.loc_info), snip);   
		POIItemizedOverlay overlay = new POIItemizedOverlay(getContext().getResources().getDrawable(R.drawable.loc_pin),getContext());
		overlay.addOverlay(overlayitem, mInfo.id, POIItemizedOverlay.POIITEM_ACTION_SHOW_BRIEF_INFO);
		
		mMapOverlays.add(overlay);
		mMapController.setCenter(orig_point);
		mMapController.animateTo(orig_point);
		mMapController.setZoom(14); 
       
		mMapView.invalidate();
	}

	@Override
	protected void onPause() {
		if(mBmapMan != null)
		{
			mBmapMan.stop();
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		if(mBmapMan != null)
		{
			mBmapMan.start();
		}
		super.onResume();
	}

	@Override
	protected boolean isRouteDisplayed() {
		
		return false;
	}
	
	private OnClickListener mExitListener = new OnClickListener()
    {

		@Override
		public void onClick(View v) {
			
			finish();
		}
    	
    };

	@Override
	protected void onDestroy() {
		
		if(mBmapMan != null)
		{
			mBmapMan.destroy();
			mBmapMan = null;
		}
		
		super.onDestroy();
	}
    
    
}
